﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Admin.SystemSet
{
    public partial class RoleAdvertRadioConfig : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }


        #region 加载列表
        private void LoadContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            PageConfig(RoleUPager);


            //List<Member> list = new List<Member();
            RoleHelper rhelper = new RoleHelper();
            //加载前台用户权限
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "RoleType", 1);

            RoleUPager.ItemCount = rhelper.QueryRoleCountBySearch(c);

            // 以下的值从BasePage中得到
            RoleUPager.UrlFormat = UrlFormat;
            RoleUPager.PrefixText = GetPreFixText(RoleUPager);

            if (mark)
            {
                PagerInit(RoleUPager);
            }

            string[] fields = new string[] { "ID", "RoleName", "PVRatio", "AdvertRatio", "CashMoney", "UpgradeBlogPVNum", "CreateDate" };

            DataTable dt = new DataTable();
            dt = rhelper.QueryRoleBySearchToTable(c, new Order[] { new Order("CreateDate") }, RoleUPager.Begin - 1, RoleUPager.Count, fields);

            rptRole.DataSource = dt;
            rptRole.DataBind();
        }
        #endregion

        #region 列表事件
        protected void rptRole_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            TextBox tbPVRatio = (TextBox)e.Item.FindControl("txtPVRatio");
            TextBox tbAdvertRadio = (TextBox)e.Item.FindControl("txtAdvertRadio");
            TextBox tbRoleName = (TextBox)e.Item.FindControl("txtRoleName");
            TextBox tbCashMoney = (TextBox)e.Item.FindControl("txtCashMoney");
            TextBox tbUpgradeBlogPVNum = (TextBox)e.Item.FindControl("txtUpgradeBlogPVNum");

            string id = hidID.Value;


            if (e.CommandName == "edit")//编辑
            {
                string advertradio = tbAdvertRadio.Text.Trim();
                string pvradio = tbPVRatio.Text.Trim();
                string strcashmoney = tbCashMoney.Text.Trim();
                string strupgradeblogpvnum = tbUpgradeBlogPVNum.Text.Trim();

                string rolename = tbRoleName.Text.Trim();
                Role rinfo = new Role();
                RoleHelper rhelper = new RoleHelper();
                rinfo = rhelper.GetRole(id);

                rinfo.ID = id;
                float result=0;
                if (!float.TryParse(pvradio, out result))
                {
                    Tools.Alert("单位PV分红金额必须为数字！");
                    return;
                }
                if (!float.TryParse(advertradio, out result))
                {
                    Tools.Alert("单次广告点击金额必须为数字！");
                    return;
                }

                int cashmoney = 0;
                if (!Int32.TryParse(strcashmoney, out cashmoney))
                {
                    Tools.Alert("取现最低金额必须为数字！");
                    return;
                }

                int upgradeblogpvnum = 0;
                if (!Int32.TryParse(strupgradeblogpvnum, out upgradeblogpvnum))
                {
                    Tools.Alert("升级最低PVNum必须为数字！");
                    return;
                }

                rinfo.PVRatio = pvradio;
                rinfo.AdvertRatio = advertradio;
                rinfo.CashMoney = cashmoney;
                rinfo.UpgradeBlogPVNum = upgradeblogpvnum;
                rinfo.RoleName = rolename;
                rhelper.EditRole(rinfo);

                Tools.Alert("修改成功！");
            }

            LoadContent(false);
        }
        #endregion
    }
}